#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <queue>

using namespace std;

map<string, int> m;
vector<int> v[501];
queue<int> q;


string s[501];
bool mark[501], no;
string a, b;
int n, i, j, t, x, y, k, des, pre[502];


bool read(){
	k=0;
	if(scanf("%d", &n)!=1)return 0;
	
	m.clear();
	for(i=0;i<501;i++)v[i].clear();
	
	for(i=0;i<n;i++){
		cin>>a>>b>>t;
		if(m[a]==0){
			m[a]=++k;
			x=k;
			s[x]=a;
		}else x = m[a];
		if(m[b]==0){
			m[b]=++k;
			y=k;
			s[y]=b;
		}else y = m[b];
		
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for(i=1;i<=k;i++){
		printf("%12s-", s[i].c_str());
		for(j=0;j<v[i].size();j++)printf(" %12s", s[ v[i][j] ].c_str());
		printf("\n");
	}
	
	
	return 1;
}
void process(){
	
	cin>>a>>b;
	x=m[a];
	y=m[b];
	if(!x || !y){
		printf("No valid route.\n\n");
		return;
	}
	no = 1;
	des=y;
	for(i=1;i<=k;i++)mark[i]=1;
	q.push(x);
	mark[x]=0;
	pre[x]=501;
	pre[501]=0;
	
	while(!q.empty()){
		x = q.front();q.pop();
		
		for(i=0;i<v[x].size()&&no;++i){
			y = v[x][i];
			if(mark[y]){
				pre[y]=x;
				q.push(y);
				mark[y]=0;
				if(des==y)no=0;
			}
		}
	}
	if(no)printf("No valid route.\n\n");
	
	else {
		while(pre[des]){
			printf(" %s", s[des].c_str() );
			des=pre[des];
		}
		
		printf("\n\n");
	}
}
int main(){
	freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);
	while(read())process();
	return 0;
}
